﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Zoo.Cat.Domain.Entities;
using Zoo.Cat.Domain.Entities.Jobseekers;
using System.Web;
using Zoo.Cat.Application.Jobseekers;
using Zoo.Cat.Application.Authentication;
using Zoo.Infrastructure;

namespace Zoo.Cat.Web.Framework {
    public partial class WorkContext : IWorkContext {

        private const string UserCookieName = "Cat.user";
        #region fields

        private readonly HttpContextBase _httpContext;
        private readonly IJobseekerService _jobseekerService;
        private readonly IAuthenticationService _authenticationService;
        //private readonly IWebHelper _webHelper;
        private  Jobseeker _cachedUser;
        #endregion

        #region Ctor
        public WorkContext(HttpContextBase httpContext,
             IAuthenticationService authenticationService, //WebHelper webHelper,
             IJobseekerService jobseekerService) {
                 _httpContext = httpContext;
                 _authenticationService = authenticationService;
                // _webHelper = webHelper;
                 _jobseekerService = jobseekerService;
        }
        #endregion

        protected Jobseeker GetCurrentUser() {
            if (_cachedUser != null)
                return _cachedUser;

            Jobseeker user = null;
            if (_httpContext != null) {

                // registered user
                if (user == null) {
                    user = _authenticationService.GetAuthenticatedUser();
                }

                //user  = new Jobseeker {
                //    JobseekerId = Guid.NewGuid(),
                //    Email = "jobseeker@myhome.com",
                //    Active = true,
                //    Username = "yidiao",
                //};

                _cachedUser = user;
            }

            return _cachedUser;
        }

        protected HttpCookie GetUserCookie() {
            if (_httpContext == null || _httpContext.Request == null)
                return null;

            return _httpContext.Request.Cookies[UserCookieName];
        }

        protected void SetUserCookie(Guid userGuid) {
            var cookie = new HttpCookie(UserCookieName);
            cookie.Value = userGuid.ToString();
            if (userGuid == Guid.Empty) {
                cookie.Expires = DateTime.Now.AddMonths(-1);
            }
            else {
                int cookieExpires = 24 * 365; //TODO make configurable
                cookie.Expires = DateTime.Now.AddHours(cookieExpires);
            }

            if (_httpContext != null && _httpContext.Response != null) {
                _httpContext.Response.Cookies.Remove(UserCookieName);
                _httpContext.Response.Cookies.Add(cookie);
            }
        }

        public Jobseeker CurrentUser {
            get {
                return GetCurrentUser();
            }
            set {
                _cachedUser = value;
            }
        }
    }
}
